﻿myApp.controller("recentTransactionController", function ($scope, $http, $modal, ngTableParams, $routeParams, $filter, authService, $location, $window,$rootScope) {
    document.title = "Penang Realty 世界地產 Property Transacted History";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 Property Transacted History', 'description': 'Free search for real transaction histories in Penang.' });
    $scope.data.searchData = [];


    //$scope.warning = "";

    //$scope.error = "";

    //$scope.info = "";

    function pageInit() {
        $http.get('api/RecentTransaction/GetDistrict')
             .success(function (data) {         
                 console.log(data);
                 $scope.districts = data;
             }
            )
    }
    $scope.propertyType = [{"sqlName":"INDUSTRI", "displayName":"INDUSTRIAL"},
                           {"sqlName":"PEMBANGUNAN", "displayName":"DEVELOPMENT"},
                           { "sqlName": "PERTANIAN", "displayName": "AGRICULTURE" },
                           { "sqlName": "PERDAGANGAN", "displayName": "COMMERCIAL" },
                           { "sqlName": "KEDIAMAN", "displayName": "RESIDENTIAL" },
                           { "sqlName": "LAIN-LAIN", "displayName": "OTHERS" }];

    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 25,           // count per page
        filter: {
        }

    }, {
        total: $scope.data.searchData.length, // length of data
        getData: function ($defer, params) {

            var orderedProperty = params.filter() ? $filter('filter')($scope.data.searchData, params.filter()) : $scope.data.searchData;
            // use build-in angular filter
            orderedProperty = params.sorting() ? $filter('orderBy')(orderedProperty, params.orderBy()) : orderedProperty;
            params.total(orderedProperty.length); // set total for recalc pagination
            $defer.resolve(orderedProperty.slice((params.page() - 1) * params.count(), params.page() * params.count()));

        }
    })

    $scope.$watch('data.searchData', function (newValue, oldValue) {
        if ($scope.tableParams) $scope.tableParams.reload();
    });

    $scope.search = function () {

        $scope.warning = "";
        $scope.error = "";
        $scope.info = "";

        console.log($scope.property);

        if (validateSearch() == false) return;

        $scope.loader.loading = true;
        if ($scope.tableParams !== undefined) $scope.tableParams.reload();
        
        var url;
        if ($scope.property.saleType == "S") {
            url = 'api/RecentTransaction/GetRecentSale';
        } else {
            url = 'api/RecentTransaction/GetRecentRent'
        }
        $http.post(url, $scope.property)
            .success(function (result) {
                $scope.loader.loading = false;
                $scope.data.searchData = result;
                console.log(result);
            }
        ).error(function (result) {
            $scope.loader.loading = false;
            console.log("error getting data from server...");
            $scope.error = "Error getting data from server.";

        })
    }

    $scope.clearEntry = function () {
        $scope.property.keyword = "";
        $scope.property.saleType = "";
        $scope.property.type = "";
        $scope.property.district = "";
        $scope.data.searchData = [];
    }

    validateSearch = function()
    {
        console.log("Validate Search");
        console.log($scope.property);
        var returnval = true;
        var errorval = "";

        if ($scope.property.saleType == "" || $scope.property.saleType == "Select") {
            errorval = "Transaction Type, ";
            returnval= false;
        }
        if (!$scope.property.hasOwnProperty("district")) {
            // make it non compulsory
            $scope.property.district = "";
            //errorval = errorval + "District, ";
            //returnval= false;
        }
        if (!$scope.property.hasOwnProperty("type")) {
            errorval = errorval + "Property Type, ";
            returnval = false;
        }

        if (!$scope.property.hasOwnProperty("keyword") || $scope.property.keyword == "" || typeof $scope.property.keyword === 'undefined') {
            errorval = errorval + "Keyword ";
            returnval = false;
        }
        if (returnval == false){
            $scope.error = "Ensure these items are not empty: " + errorval;
        }
        return returnval;
        // check if all input are selected.
    }

    pageInit();

   
})


